<?php
$auth = isset($_COOKIE['auth']) ? json_decode($_COOKIE['auth'],true) : [];

if(!$auth)
{
    notice('请先登录','login.php');
}

$id = $auth['id'] ?? 0;
$username = $auth['username'] ?? '';

$CheckSql = "SELECT * FROM `pre_admin` WHERE `id` = '$id' AND `username` = '$username'";

$Auth = find($CheckSql);

if(!$Auth)
{
    setcookie('auth','',time() - 1);
    notice('非法登录','login.php');
}

?>
<div class="navbar">
    <div class="navbar-inner">
        <ul class="nav pull-right">
            <li>
                <a role="button">
                    <?php if(is_file('.' . $Auth['avatar'])){ ?>
                        <img src="<?php echo $Auth['avatar']; ?>" style="width: 20px;height:20px;border-radius: 2px;object-fit:cover;" alt="">
                    <?php }else{ ?>
                        <i class="icon-user"></i>
                    <?php }?>
                    <?php echo $Auth['username']; ?>
                </a>
            </li>
            <li>
                <a id="logout" class="hidden-phone visible-tablet visible-desktop" role="button">Logout</a>
            </li>
        </ul>
        <a class="brand" href="index.html"><span class="second">Admin</span></a>
    </div>
</div>

<script>
    $('#logout').click(function(){
        let res = confirm('是否确认当前账号？');

        if(res === false) return false;

        $.ajax({
            type:'post',
            url:'api.php',
            data:{
                action:'logout'
            },
            dataType:'json',
            success:(result) =>
            {
                if(result.code === 1)
                {
                    alert(result.msg);

                    location.href = 'login.php';
                }
            }
        })
    })
</script>